package com.tz.farmer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tz.farmer.model.FarmerActives;
import com.tz.farmer.model.FarmerActivesResult;
import com.tz.farmer.so.BaseSo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface FarmerActivesMapper extends BaseMapper<FarmerActives> {
    int deleteByPrimaryKey(Integer id);

    int insert(FarmerActives record);

    int insertSelective(FarmerActives record);

    FarmerActives selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(FarmerActives record);

    int updateByPrimaryKey(FarmerActives record);

    @Select("select id,title,active_text,join_people,phone,address,start_time,end_time,active_end_time,'1' as ifEnroll from farmer_actives where id in(select activity_id from enroll_info where open_id=#{openId}) and is_sued=2 and property=#{flag}" +
            " UNION all\n" +
            " select id,title,active_text,join_people,phone,address,start_time,end_time,active_end_time,'0' as ifEnroll from farmer_actives where id not in(select activity_id from enroll_info where open_id=#{openId}) and is_sued=2 and property=#{flag}" )
    List<FarmerActivesResult> selectActivityInfos(BaseSo baseSo);

    /**
     * kai  统计
     * @param flag
     * @param keyword
     * @param timeStart
     * @param timeEnd
     * @return
     */
     @Select("<script>" +
            "select   \n" +
            "count(1) \n" +
            "from farmer_actives \n" +
            "where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \" >and ( title like CONCAT('%', #{keyword},'%') \n" +
            " or address like CONCAT('%', #{keyword},'%') ) </if> \n" +
            " <if test=\"timeStart != null and timeStart.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${timeStart} 00:00:00', '%Y-%m-%d %H:%i:%s')   \n" +
            "      and STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${timeEnd} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            "</script>")
    int countFlag(@Param("flag")Integer flag,@Param("keyword") String keyword,@Param("timeStart") String timeStart, @Param("timeEnd")String timeEnd);

    /**
     * kai 条件查询
     * @param flag
     * @param keyword
     * @param timeStart
     * @param timeEnd
     * @param n
     * @param m
     * @return
     */
    @Select("<script>" +
            "select   \n" +
            " id, \n" +
            " title, \n" +
            " active_text, \n" +
            " join_people, \n" +
            " phone, \n" +
            " address, \n" +
            " start_time, \n" +
            " end_time, \n" +
            " release_time, \n" +
            " active_end_time, \n" +
            " is_sued, \n" +
            " top, \n" +
            " property\n" +
            " FROM farmer_actives \n" +
            " where property = #{flag} and 1 = 1  " +
            " <if test = \"keyword != null and keyword.trim() !=''  \" >and (title like CONCAT('%', #{keyword},'%')  \n" +
            " or address like CONCAT('%', #{keyword},'%') ) </if> \n" +
            " <if test=\"timeStart != null and timeStart.trim() != ''\">" +
            "   <![CDATA[   and ( STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') >=  STR_TO_DATE( '${timeStart} 00:00:00', '%Y-%m-%d %H:%i:%s')    \n" +
            "      and STR_TO_DATE(release_time, '%Y-%m-%d %H:%i:%s') <= STR_TO_DATE( '${timeEnd} 23:59:59', '%Y-%m-%d %H:%i:%s')  )  ]]> \n" +
            " </if> " +
            " order by top DESC , release_time DESC limit #{n} , #{m} " +
            "</script>")
    List<FarmerActives> selectConditionList(@Param("flag")Integer flag,@Param("keyword") String keyword,@Param("timeStart") String timeStart, @Param("timeEnd")String timeEnd, @Param("n")int n,@Param("m") int m);

    /**
     * kai 置顶之前查询top最大值
     * @param flag
     * @return
     */
    @Select("SELECT  " +
            "\tmax( top ) AS maxTop \n" +
            "FROM\n" +
            "\tfarmer_actives \n" +
            "WHERE\n" +
            "\tproperty = #{flag}")
    int selectMaxTop(@Param("flag") Integer flag);

}